package com.yunshipei.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yunshipei.db.DatabaseUtil;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.ShareItemEnum;
import com.yunshipei.model.ShareModel;

import io.rong.app.model.GroupCreateInfo;
import io.rong.imlib.model.Conversation;

/**
 * Created by cike on 16/8/17.
 */
public class YspConversationListAdapter extends EnterBaseAdapter<ShareModel> {
    LayoutInflater mLayoutInflater;
    Context mContext;
    private DatabaseUtil databaseUtil;

    public YspConversationListAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        databaseUtil = new DatabaseUtil();
    }

    ViewHolderGroup holder = null;
    GroupContactHolder gcHolder = null;
    SmallTitleHolder stHolder = null;
    ShareModel shareModel = null;
    ShareItemEnum itemType = null;
    View view = null;

    @Override
    protected View newView(Context context, int position, ViewGroup group) {
        shareModel = getItem(position);
        itemType = shareModel.getItemType();
        if (itemType == ShareItemEnum.CONVERSATION) {
            view = mLayoutInflater.inflate(R.layout.common_item, null);
            holder = new ViewHolderGroup();
            view.findViewById(R.id.avatarText).setVisibility(View.GONE);
            holder.avatar = (ImageView) view.findViewById(R.id.avatar);
            holder.nameTV = (TextView) view.findViewById(R.id.name);
            holder.numberTV = (TextView) view.findViewById(R.id.number);
            holder.numberTV.setVisibility(View.GONE);
            view.setTag(holder);
            holder = null;
        } else if (itemType == ShareItemEnum.CONTACT || itemType == ShareItemEnum.GROUP) {
            gcHolder = new GroupContactHolder();
            view = mLayoutInflater.inflate(R.layout.contact_share_item, null);
            gcHolder.textView = (TextView) view.findViewById(R.id.name);
            view.setTag(gcHolder);
            gcHolder = null;
        } else if (itemType == ShareItemEnum.SMALLTITLE) {
            view = mLayoutInflater.inflate(R.layout.small_title_item, null);
            stHolder = new SmallTitleHolder();
            stHolder.textView = (TextView) view.findViewById(R.id.name);
            view.setTag(stHolder);
            stHolder = null;
        }
        return view;
    }

    @Override
    protected void bindView(View v, int position, ShareModel shareModel) {
        if (shareModel == null)
            return;
        if (v.getTag() instanceof ViewHolderGroup) {
            holder = (ViewHolderGroup) v.getTag();
        } else if (v.getTag() instanceof GroupContactHolder) {
            gcHolder = (GroupContactHolder) v.getTag();
        } else if (v.getTag() instanceof SmallTitleHolder) {
            stHolder = (SmallTitleHolder) v.getTag();
        }
        if (shareModel.getItemType() == ShareItemEnum.CONVERSATION) {
            Conversation data = (Conversation) shareModel.getItemContent();
            if (data.getConversationType() == Conversation.ConversationType.GROUP) {
                GroupCreateInfo groupCreateInfo = databaseUtil.findGroupById(data.getTargetId());
                holder.nameTV.setText(groupCreateInfo != null ? groupCreateInfo.getName() : "");
                holder.avatar.setTag(data.getPortraitUrl());
                Glide.with((Activity) mContext).load(groupCreateInfo.getPortraitUri()).placeholder(R.drawable.group).into(holder.avatar);
                Log.e("avator", data.getPortraitUrl());
            } else if (data.getConversationType() == Conversation.ConversationType.PRIVATE) {
                io.rong.imlib.model.UserInfo userInfo = databaseUtil.queryByUuid(data.getTargetId());
                holder.nameTV.setText(userInfo != null ? userInfo.getName() : "");
                holder.avatar.setTag(data.getPortraitUrl());
                Glide.with((Activity) mContext).load(userInfo == null ? "" : userInfo.getPortraitUri()).placeholder(R.drawable.de_default_portrait).into(holder.avatar);
                Log.e("avator", data.getPortraitUrl());
            }
            v.setTag(holder);
        } else if (shareModel.getItemType() == ShareItemEnum.CONTACT || shareModel.getItemType() == ShareItemEnum.GROUP) {
            String name = (String) shareModel.getItemContent();
            gcHolder.textView.setText(name);
            v.setTag(gcHolder);
        } else if (shareModel.getItemType() == ShareItemEnum.SMALLTITLE) {
            String name = (String) shareModel.getItemContent();
            stHolder.textView.setText(name);
            v.setTag(stHolder);
        }
    }

    @Override
    public int getItemViewType(int position) {
        shareModel = getItem(position);
        if (shareModel == null)
            return 0;
        return shareModel.getItemType().getPrice();

    }

    @Override
    public int getViewTypeCount() {
        return ShareItemEnum.values().length;
    }


    class ViewHolderGroup {
        TextView nameTV, numberTV;
        ImageView avatar;

    }

    class GroupContactHolder {
        TextView textView;

    }

    class SmallTitleHolder {
        TextView textView;
    }
}
